package com.hb.trade.server.center.sys.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hb.core.pojo.center.sys.dto.ConsoleItemDTO;
import com.hb.core.pojo.center.sys.entity.ConsoleItem;
import com.hb.core.pojo.center.sys.entity.ConsoleItemStatus;
import com.hb.trade.server.center.sys.service.ConsoleItemService;
import com.hb.trade.server.center.sys.service.ConsoleItemStatusService;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.MsgUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * 操作台控制按钮信息表 前端控制器
 * 该类中接口为后台管理调用，不对外开放
 *
 * @author zhaojk
 * @since 2021-11-29
 */
@RestController
public class ConsoleItemController {

    protected final ConsoleItemService consoleItemService;
    protected final ConsoleItemStatusService consoleItemStatusService;

    public ConsoleItemController(ConsoleItemService consoleItemService,
                                 ConsoleItemStatusService consoleItemStatusService) {
        this.consoleItemService = consoleItemService;
        this.consoleItemStatusService = consoleItemStatusService;
    }

    /**
     * 获取所有操作台按钮
     *
     * @return
     */
    @PostMapping("/consoleItem/itemList")
    public Message<List<ConsoleItem>> itemList() {
        QueryWrapper<ConsoleItem> query = ConsoleItem.wrapper();
        query.orderByAsc("sort_code");
        List<ConsoleItem> items = consoleItemService.list(query);
        return MsgUtil.success(items);
    }

    /**
     * 新增控制台按钮
     *
     * @param item
     * @return
     */
    @PostMapping("/consoleItem/add")
    public Message<Boolean> add(@RequestBody ConsoleItem item) {
        String operaName = item.getOperaName();
        if (StrUtil.isEmpty(operaName)) {
            return MsgUtil.fail("操作项名称不能为空");
        }
        QueryWrapper<ConsoleItem> query = ConsoleItem.wrapper();
        query.eq("opera_name", operaName);

        long count = consoleItemService.count(query);
        if (count > 0) {
            return MsgUtil.fail("操作项名称已经存在");
        }
        item.buildBaseData(1);
        if (item.getParentId() == null) {
            item.setParentId(0L);
        }
        BigDecimal maxSort = consoleItemService.maxSort(item.getParentId());
        item.setSortCode(maxSort.add(BigDecimal.ONE));
        boolean save = consoleItemService.save(item);
        return MsgUtil.success(save);
    }

    /**
     * 修改控制台按钮信息
     *
     * @param item
     * @return
     */
    @PostMapping("/consoleItem/update")
    public Message<Boolean> update(@RequestBody ConsoleItem item) {
        item.buildBaseData(2);
        boolean update = consoleItemService.updateById(item);
        return MsgUtil.success(update);
    }

    /**
     * 删除控制台按钮
     *
     * @param item
     * @return
     */
    @PostMapping("/consoleItem/delete")
    public Message<Boolean> delete(@RequestBody ConsoleItem item) {
        item.setDeleteFlag(1);
        boolean remove = consoleItemService.updateById(item);

        QueryWrapper<ConsoleItemStatus> wrapper = Wrappers.query();
        wrapper.eq("item_id", item.getId());
        boolean removeStatus = consoleItemStatusService.remove(wrapper);
        return MsgUtil.success(remove && removeStatus);
    }

    /**
     * 拖动控制台按钮以改变层级和位置
     *
     * @param item
     * @return
     */
    @PostMapping("/consoleItem/transferTo")
    public Message<Boolean> transferTo(@RequestBody ConsoleItemDTO item) {
        Long id = item.getId();
        if (id == null) {
            return MsgUtil.fail("id is null");
        }
        Long targetId = item.getTargetId();
        if (targetId == null) {
            return MsgUtil.fail("targetId is null");
        }
        String moveType = item.getMoveType();
        if (StrUtil.isEmpty(moveType)) {
            return MsgUtil.fail("moveType无效");
        }
        boolean update;
        if ("inner".equals(moveType)) {
            ConsoleItem consoleItem = consoleItemService.getById(id);
            consoleItem.setParentId(targetId);
            update = consoleItemService.updateById(consoleItem);
            return MsgUtil.success(update);
        }

        ConsoleItem targetNode = consoleItemService.getById(targetId);
        ConsoleItem targetParentNode = consoleItemService.getById(targetNode.getParentId());

        if (targetParentNode == null) {
            targetParentNode = new ConsoleItem();
            targetParentNode.setId(0L);
        }

        item.setParentId(targetParentNode.getId());
        BigDecimal targetNodeSort = targetNode.getSortCode();

        if ("prev".equals(moveType)) {
            BigDecimal subtract = targetNodeSort.subtract(BigDecimal.valueOf(0.1D));
            item.setSortCode(subtract);
        }
        if ("next".equals(moveType)) {
            BigDecimal add = targetNodeSort.add(BigDecimal.valueOf(0.1D));
            item.setSortCode(add);
        }

        update = consoleItemService.updateById(item);
        update &= consoleItemService.resort(targetParentNode.getId());
        return MsgUtil.success(update);
    }

    /**
     * 获取所有控制台按钮, 带选中状态
     *
     * @param itemStatus
     * @return
     */
    @PostMapping("/consoleItem/itemChecked")
    public Message<List<ConsoleItemDTO>> itemChecked(@RequestBody ConsoleItemStatus itemStatus) {
        List<ConsoleItemDTO> items = consoleItemService.itemChecked(itemStatus.getStatusId());
        return MsgUtil.success(items);
    }

    /**
     * 点击按钮时显示该按钮对应的招标方式和角色
     *
     * @param consoleItemId
     * @return
     */
    @PostMapping("/consoleItem/getRoleSignsAndBidWay")
    public Message<ConsoleItem> getRoleSignsAndBidWay(Long consoleItemId) {
        if (consoleItemId == null) {
            return MsgUtil.fail("缺少参数");
        }
        QueryWrapper<ConsoleItem> wrapper = ConsoleItem.wrapper("bid_ways", "role_signs", "create_time", "update_time");
        wrapper.eq("id", consoleItemId);
        ConsoleItem consoleItem = consoleItemService.getOne(wrapper);
        if (consoleItem == null) {
            return MsgUtil.fail("没有数据");
        }
        return MsgUtil.success(consoleItem);
    }

    /**
     * 更新标段状态和控制台按钮的关联关系
     *
     * @param item
     * @return
     */
    @PostMapping("/consoleItem/updateRelation")
    public Message<Boolean> updateRelation(@RequestBody ConsoleItemDTO item) {
        Long statusId = item.getStatusId();
        if (statusId == null) {
            return MsgUtil.fail("statusId无效");
        }
        List<Long> itemIds = item.getItemIds();
        if (CollUtil.isEmpty(itemIds)) {
            return MsgUtil.fail("itemIds无效");
        }
        boolean result = consoleItemService.updateRelation(statusId, itemIds);
        return MsgUtil.success(result);
    }

    /**
     * 修改指定按钮对应的企业角色和招标方式
     *
     * @param itemId    按钮ID
     * @param roleSigns 角色ID, 逗号分隔的字符串
     * @param bidWays   招标方式
     * @return
     */
    @PostMapping("/consoleItem/updateItemRolesAndBidWays")
    public Message<Boolean> updateItemRolesAndBidWays(Long itemId,
                                                      String roleSigns,
                                                      String bidWays) {
        if (itemId == null || StrUtil.isEmpty(roleSigns)) {
            return MsgUtil.fail("缺少参数");
        }
        ConsoleItem consoleItem = consoleItemService.getById(itemId);
        if (consoleItem == null) {
            return MsgUtil.fail("没有按钮信息");
        }

        consoleItem.setRoleSigns(roleSigns);
        consoleItem.setBidWays(bidWays);
        consoleItem.buildBaseData(2);

        boolean update = consoleItemService.updateById(consoleItem);
        return MsgUtil.success(update);
    }

}
